package com.odianyun.hello.service.impl;

import com.google.common.collect.Lists;
import com.odianyun.db.mybatis.EntityQueryParam;
import com.odianyun.db.mybatis.QueryParam;
import com.odianyun.db.query.PageVO;
import com.odianyun.hello.constant.SoConstant;
import com.odianyun.hello.mapper.SoItemMapper;
import com.odianyun.hello.mapper.SoPackageItemMapper;
import com.odianyun.hello.mapper.SoPackageMapper;

import com.odianyun.hello.mapper.SoReturnItemMapper;
import com.odianyun.hello.model.DTO.SoPackageDTO;
import com.odianyun.hello.model.PO.*;
import com.odianyun.hello.model.VO.SoPackageItemVO;
import com.odianyun.hello.model.VO.SoPackageVO;
import com.odianyun.hello.service.SoPackageService;
import com.odianyun.project.base.IEntity;
import com.odianyun.project.query.PageQueryArgs;
import com.odianyun.project.query.QueryArgs;
import com.odianyun.project.support.base.db.EQ;
import com.odianyun.project.support.base.db.Q;
import com.odianyun.project.support.base.service.OdyEntityService;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SoPackageServiceImpl extends OdyEntityService<SoPackagePO, SoPackageVO, PageQueryArgs, QueryArgs, SoPackageMapper> implements SoPackageService {
    @Resource
    private SoPackageMapper mapper;
    @Resource
    private SoPackageItemMapper soPackageItemMapper;
    @Resource
    private SoReturnItemMapper soReturnItemMapper;
    private SoItemMapper soItemMapper;
    @Override
    protected SoPackageMapper getMapper() {
        return mapper;
    }

    @Override
	protected void afterListPage(PageVO<? extends IEntity> pageVO) {
	    for (IEntity entity : pageVO.getList()) {
	        SoPackageVO vo = (SoPackageVO) entity;
            vo.setPackageCode("packageCode");
            vo.setOrderCode("orderCode");
	    }
	}

    @Override
    public List<SoPackagePO> getByNameRegex(String regexName) {
        QueryParam queryParam = new QueryParam();
        queryParam.select("goodReceiverAddress");
        queryParam.like("goodReceiverAddress",regexName);
        List<SoPackagePO> list = mapper.list(queryParam);
        return list;
    }

    @Override
    public void updateStatusForSignWithTx(SoPackageDTO dto) {
        //通过包裹状态和订单号查询
        Long packageId = dto.getId();
        SoPackageVO soPackageVO =getById(packageId);
        if (soPackageVO == null){
            System.out.println("包裹不存在");
        }
        if(soPackageVO.getPackageStatus().compareTo(SoConstant.PACKAGE_STATUS_ARRIVED) >=0){
            System.out.println("包裹状态不正确");
        }
        String orderCode = soPackageVO.getOrderCode();
        String packageCode = soPackageVO.getPackageCode();

        //获取该订单下包裹状态为3040的包裹
        QueryParam queryParam = new Q("id","packageCode");
        queryParam.eq("packageStatus",SoConstant.PACKAGE_STATUS_ARRIVED)
                .eq("orderCode",orderCode);
        List<SoPackageVO> soPackageSignList =list(queryParam);

        //获取该包裹下的所有商品
        List<SoPackageItemPO> soPackageItemVOList =soPackageItemMapper.list(new Q("id","soItemId","productItemNum","productItemOutNum")
                .eq("packageCode",packageCode));

        //获取其他已签收的包裹
        List<SoPackageItemPO> restList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(soPackageSignList)){
            QueryParam queryParamSoPackageItem = new Q("soItemId","productItemOutNum");
            List<String> codeList = soPackageSignList.stream()
                    .map(SoPackageVO::getPackageCode)
                    .collect(Collectors.toList());
            restList = soPackageItemMapper.list(queryParamSoPackageItem.in("packageCode",codeList));
        }
        restList.addAll(soPackageItemVOList);


        //获取已审核通过和已完成的售后单明细
        EntityQueryParam sri = new EntityQueryParam(SoReturnItemPO.class,"sri").selects("soItemId","returnProductItemNum ");
        EntityQueryParam sr  = new EQ(SoReturnPO.class,"sr");
        sri.join(sr).on("sri.returnId","sr.id");
        sri.eq("orderCode",orderCode).in("sr.returnStatus", Lists.newArrayList(SoConstant.PACKAGE_STATUS_ARRIVED,SoConstant.PACKAGE_STATUS_FINISHED));
        List<SoReturnItemPO> sriList = soReturnItemMapper.list(sri);

        Map<Long,BigDecimal> soReturnItemMap = sriList.stream().collect(Collectors
                .groupingBy(SoReturnItemPO::getReturnId, Collectors
                        .reducing(BigDecimal.ZERO,
                                SoReturnItemPO::getReturnProductItemNum,BigDecimal::add)));

        //获取订单商品明细
        QueryParam soItem = new Q("id","produceItemNum");
        List<SoItemPO> siList = soItemMapper.list(soItem.eq("orderCode",orderCode));

        //已签收商品行
        Integer signItemCount = soItemMapper.count(soItem.eq("orderStatus",SoConstant.ORDER_STATUS_RECIEVE));

        Map<Long , BigDecimal> soItemMap = siList.stream()
                .collect(Collectors
                        .toMap(SoItemPO::getId,SoItemPO::getProductItemNum));

        List<SoItemPO> needUpdateItem = new ArrayList<>();

        Map<Long,BigDecimal> packageItemNumMap = restList.stream()
                .collect(Collectors.groupingBy(SoPackageItemPO::getSoItemId,
                        Collectors.reducing(BigDecimal.ZERO,SoPackageItemPO::getProductItemNum
                                ,BigDecimal::add)));



    }
}
